我想重载<<运算符,请指教如何实现
来源:百度知道 编辑:UC知道 时间:2024/08/21 22:43:32
如题
class Vector
{
int x,y;
public:
Vector()
{
};
Vector(int x1,int y1)
{
x=x1;
y=y1;
}
void operator <<(Vector)
{
cout<<"("<<x<<","<<y<<")"<<endl;
}///////////////////////////////////////////这里是错误的
//////////////////////////我想实现的功能是vector v1;cout<<v1;
void display()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
};谢谢指教
谢谢:
那>>怎嘛重载啊?
class Vector
{
int x,y;
public:
Vector()
{
};
Vector(int x1,int y1)
{
x=x1;
y=y1;
}
void operator <<(Vector)
{
cout<<"("<<x<<","<<y<<")"<<endl;
}///////////////////////////////////////////这里是错误的
//////////////////////////我想实现的功能是vector v1;cout<<v1;
void display()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
};谢谢指教
谢谢:
那>>怎嘛重载啊?
friend std::ostream& operator<<(std::ostream& os, const Vector &v)
{
return std::cout<<"("<<v.x<<","<<v.y<<")"<<std::endl;
}
#include<iostream>
using namespace std;
class Vector
{
int x,y;
public:
Vector();
Vector(int x1,int y1)
{
x=x1;
y=y1;
}
friend ostream& operator <<(ostream &out,Vector &v)
{
out<<"("<<v.x<<","<<v.y<<")"<<endl;
return out;
}///////////////////////////////////////////这里是错误的
//////////////////////////我想实现的功能是vector v1;cout<<v1;
void display()
{
cout<<"("<<x<<","<<y<<")"<<endl;
}
};
int main()
{
Vector v(1,2);
cout<<v<<endl;
return 0;
}